package org.example.controller;


import org.example.controller.request.AccountLoginRequest;
import org.example.controller.request.AccountRegisterRequest;
import org.example.enums.BizCodeEnum;
import org.example.service.AccountService;
import org.example.service.FileService;
import org.example.util.JsonData;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谢超
 * @since 2023-07-16
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Resource
    private FileService fileService;

    @Resource
    private AccountService accountService;

    /**
     * 文件上传最大默认1M
     * @param file
     * @return
     */
    @PostMapping("upload")
    public JsonData uploadUserImg (@RequestPart("file")MultipartFile file) {
        String res = fileService.uploadUserImg(file);

        return res != null ? JsonData.buildSuccess(res) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     * 用户注册
     * @param registerRequest
     * @return
     */
    @PostMapping("register")
    public JsonData register (@RequestBody AccountRegisterRequest registerRequest) {
        JsonData jsonData = accountService.register(registerRequest);
        return jsonData;
    }

    @PostMapping("login")
    public JsonData login (@RequestBody AccountLoginRequest request) {
        JsonData jsonData = accountService.login(request);
        return jsonData;
    }
}

